<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="发货日期" prop="deliveryDate">
      <el-date-picker v-model="dataForm.deliveryDate" type="date" placeholder="发货日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></el-date-picker>
    </el-form-item>
    <el-form-item label="业务员" prop="salesman">
      <el-input v-model="dataForm.salesman" placeholder="业务员"></el-input>
    </el-form-item>
    <el-form-item label="客户全名" prop="customerName">
      <el-input v-model="dataForm.customerName" placeholder="客户全名"></el-input>
    </el-form-item>
    <el-form-item label="单据类型" prop="documentType">
      <el-input v-model="dataForm.documentType" placeholder="单据类型代码"></el-input>
    </el-form-item>
    <el-form-item label="产品代号" prop="productCode">
      <el-input v-model="dataForm.productCode" placeholder="产品代号"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="发货数量" prop="deliveryQuantity">
      <el-input v-model="dataForm.deliveryQuantity" placeholder="发货数量"></el-input>
    </el-form-item>
    <el-form-item label="本币含税单价" prop="localCurrencyPrice">
      <el-input v-model="dataForm.localCurrencyPrice" placeholder="本币含税单价"></el-input>
    </el-form-item>
    <el-form-item label="本币含税金额" prop="localCurrencyAmount">
      <el-input v-model="dataForm.localCurrencyAmount" placeholder="本币含税金额"></el-input>
    </el-form-item>
    <el-form-item label="所属区域" prop="region">
      <el-input v-model="dataForm.region" placeholder="所属区域"></el-input>
    </el-form-item>
    <el-form-item label="发货类型" prop="deliveryType">
      <el-input v-model="dataForm.deliveryType" placeholder="发货类型代码"></el-input>
    </el-form-item>
    <el-form-item label="结算价" prop="settlementPrice">
      <el-input v-model="dataForm.settlementPrice" placeholder="结算价"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          deliveryDate: '',
          salesman: '',
          customerName: '',
          documentType: '',
          productCode: '',
          productName: '',
          deliveryQuantity: '',
          localCurrencyPrice: '',
          localCurrencyAmount: '',
          region: '',
          deliveryType: '',
          settlementPrice: ''
        },
        dataRule: {
          deliveryDate: [
            { required: true, message: '发货日期不能为空', trigger: 'blur' }
          ],
          salesman: [
            { required: true, message: '业务员不能为空', trigger: 'blur' }
          ],
          customerName: [
            { required: true, message: '客户全名不能为空', trigger: 'blur' }
          ],
          documentType: [
            { required: true, message: '单据类型不能为空', trigger: 'blur' }
          ],
          productCode: [
            { required: true, message: '产品代号不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          deliveryQuantity: [
            { required: true, message: '发货数量不能为空', trigger: 'blur' }
          ],
          localCurrencyPrice: [
            { required: true, message: '本币含税单价不能为空', trigger: 'blur' }
          ],
          localCurrencyAmount: [
            { required: true, message: '本币含税金额不能为空', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],
          deliveryType: [
            { required: true, message: '发货类型不能为空', trigger: 'blur' }
          ],
          settlementPrice: [
            { required: true, message: '结算价不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cw/invoice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deliveryDate = data.invoice.deliveryDate
                this.dataForm.salesman = data.invoice.salesman
                this.dataForm.customerName = data.invoice.customerName
                this.dataForm.documentType = data.invoice.documentType
                this.dataForm.productCode = data.invoice.productCode
                this.dataForm.productName = data.invoice.productName
                this.dataForm.deliveryQuantity = data.invoice.deliveryQuantity
                this.dataForm.localCurrencyPrice = data.invoice.localCurrencyPrice
                this.dataForm.localCurrencyAmount = data.invoice.localCurrencyAmount
                this.dataForm.region = data.invoice.region
                this.dataForm.deliveryType = data.invoice.deliveryType
                this.dataForm.settlementPrice = data.invoice.settlementPrice
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cw/invoice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'deliveryDate': this.dataForm.deliveryDate,
                'salesman': this.dataForm.salesman,
                'customerName': this.dataForm.customerName,
                'documentType': this.dataForm.documentType,
                'productCode': this.dataForm.productCode,
                'productName': this.dataForm.productName,
                'deliveryQuantity': this.dataForm.deliveryQuantity,
                'localCurrencyPrice': this.dataForm.localCurrencyPrice,
                'localCurrencyAmount': this.dataForm.localCurrencyAmount,
                'region': this.dataForm.region,
                'deliveryType': this.dataForm.deliveryType,
                'settlementPrice': this.dataForm.settlementPrice
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
